import copy
from java_book_is_heap import left_child, right_child, parent


def heap_enqueue(data, new_element, is_max=True):
    data.append(new_element)
    n = len(data)
    i = n - 1

    def swap():
        nonlocal i
        print(data[i], data[i_parent], '->', end=' ')
        data[i], data[i_parent] = data[i_parent], data[i]
        print(data[i], data[i_parent])
        i = i_parent

    while True:
        if 0 == i:
            break
        i_parent = parent(i)
        if is_max:
            if data[i] > data[i_parent]:
                swap()
            else:
                break
        else:
            if data[i] < data[i_parent]:
                swap()
            else:
                break


if __name__ == '__main__':

    def main():
        data = [20, 10, 15, 8, 7, 13, 14, 2, 5, 6]
        print(data)
        x = 15
        print('enqueue', x)
        heap_enqueue(data, x)
        print(data)

    main()
